package com.wuyou.securitydemo.pwd;

import lombok.SneakyThrows;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.io.Serializable;

/**
 * 登录令牌
 *
 * @Date 2025/4/7 13:44
 * @Author 无忧
 */
public class CustomizedPasswordAuthenticationToken extends AbstractAuthenticationToken implements Serializable {

    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    /**
     * 用户详情 UserDetails
     */
    private final Object principal;

    /**
     * 密码
     */
    private final Object credentials;

    /**
     * 设备id
     */
    private String deviceId;

//    private final Long tenantId;
//
//    private final String tenantName;
//
//    private final String companyCode;


    public CustomizedPasswordAuthenticationToken(Object principal, Object credentials, String deviceId) {
        super(null);
        this.principal = principal;
        this.credentials = credentials;
        this.deviceId = deviceId;
    }


    public CustomizedPasswordAuthenticationToken(Object principal, Object credentials) {
        super(null);
        this.principal = principal;
        this.credentials = credentials;
        setAuthenticated(false);
    }


    @Override
    public Object getPrincipal() {
        return principal;
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }


//    public Long getTenantId() {
//        return tenantId;
//    }
//
//    public String getTenantName() {
//        return tenantName;
//    }
//
//    public String getCompanyCode() {
//        return companyCode;
//    }

    @Override
    @SneakyThrows
    public void setAuthenticated(boolean isAuthenticated) {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

}
